package myLucene;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Set;
import java.util.Vector;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.TermFreqVector;

/***
 * calculates collection frequency, document frequency and inverse document
 * frequency
 * 
 * @author joo
 * @deprecated
 */

public class Frequencies {

	public static HashMap<String, HashMap<Integer, LinkedList<String>>> getFreq(
			IndexReader iReader, HashMap<String, Integer> cf,
			HashMap<String, Integer> df) throws CorruptIndexException,
			IOException {

		HashMap<String, HashMap<Integer, LinkedList<String>>> frequencies = new HashMap<String, HashMap<Integer, LinkedList<String>>>();
		// HashMap<Integer, LinkedList<String>> cfSorted = new HashMap<Integer,
		// LinkedList<String>>();
		// HashMap<Integer, LinkedList<String>> dfSorted = new HashMap<Integer,
		// LinkedList<String>>();
		System.out.println("Start FreqAcrossDoc..." + GetTime.getTime());
		// System.out.println("MaxDOc: "+iReader.maxDoc());
		for (int i = 0; i < iReader.maxDoc(); i++) {
			Document doc = iReader.document(i);
			// String[] values =doc.getValues("lemma");
			// String sent="";
			// for(String s: values){
			// sent = sent+" "+s;
			// }
			// System.out.println("FreqAcrossDocs VALUES: "+sent);
			// WindowTermVectorMapper tvm = new WindowTermVectorMapper();
			TermFreqVector tfv = iReader.getTermFreqVector(i, "lemma");
			// System.out.println("tfv SIZE: "+tfv.size());
			// iReader.getTermFreqVector(i, "lemma",tvm);

			String[] terms = tfv.getTerms();

			int[] tFreqs = tfv.getTermFrequencies();
			for (int x = 0; x < tFreqs.length; x++) {
				String term = terms[x];
				/**
				 * exludes single characters etc
				 */
				if (term.length() > 2) {
					int tFreq = tFreqs[x];

					if (cf.containsKey(term)) {
						Integer f = cf.get(term);
						cf.remove(term);
						cf.put(term, Integer.valueOf(f.intValue() + tFreq));
					} else {
						cf.put(term, Integer.valueOf(tFreq));
					}
					if (df.containsKey(term)) {
						// System.out.println("!!!!!!!!!!!!!!!!!! hier!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

						df.put(term, df.get(term) + 1);
					} else {
						df.put(term, Integer.valueOf(1));
					}
					// Integer cfValue = cf.get(term);
					// if (cfSorted.containsKey(cfValue)) {
					// LinkedList<String> v = cfSorted.get(cfValue);
					// v.add(term);
					// cfSorted.remove(cfValue);
					// cfSorted.put(cfValue, v);
					// } else {
					// LinkedList<String> v = new LinkedList<String>();
					// v.add(term);
					// cfSorted.put(cfValue, v);
					// }
					// Integer dfValue = df.get(term);// occurences
					// if (dfSorted.containsKey(dfValue)) {
					// LinkedList<String> v = dfSorted.get(dfValue);
					// v.add(term);
					// dfSorted.remove(dfValue);
					// dfSorted.put(dfValue, v);
					// } else {
					// LinkedList<String> v = new LinkedList<String>();
					// v.add(term);
					// dfSorted.put(dfValue, v);
					// }
				}
			}
		}

		// frequencies.put("cfSorted", cfSorted);
		// frequencies.put("dfSorted", dfSorted);
		return frequencies;
	}

	public static void freqToString(HashMap<String, Integer> cf,
			HashMap<String, Integer> df, String dictPath, IndexReader iReader)
			throws IOException {
		System.out.println("Start freqToString..." + GetTime.getTime());
		BufferedWriter bwCf = new BufferedWriter(new FileWriter(new File(
				dictPath + "cf")));
		Set<String> keySetCf = cf.keySet();
		Vector<String> vCf = new Vector<String>(keySetCf);
		Collections.sort(vCf);
		for (String s : vCf) {
			String dicEntry = s + " " + cf.get(s);
			if (s.matches("^([a-z]+?|[A-Z]+?).*(\\w+)") && s.length() < 41) {
				bwCf.write(dicEntry + "\n");
			}
			System.out.println("Collection Frequency: " + dicEntry);
		}
		bwCf.close();
		BufferedWriter bwDf = new BufferedWriter(new FileWriter(new File(
				dictPath + "df")));
		BufferedWriter bwIdf = new BufferedWriter(new FileWriter(new File(
				dictPath + "idf")));
		int nOfDocs = iReader.maxDoc();
		Set<String> keySetDf = df.keySet();
		Vector<String> vDf = new Vector<String>(keySetDf);
		Collections.sort(vDf);
		for (String s : vDf) {
			Integer dfVal = df.get(s);
			if (s.matches("^([a-z]+?|[A-Z]+?).*(\\w+)") && s.length() < 41) {
				Double idf = Math.log10(Integer.valueOf(nOfDocs / 1 + dfVal)
						.doubleValue());
				String dicEntryDf = s + " " + dfVal;
				String dicEntryIdf = s + " " + idf;
				bwDf.write(dicEntryDf + "\n");
				bwIdf.write(dicEntryIdf + "\n");
				System.out.println("Document Frequency: " + dicEntryDf);
			}
		}
		cf.clear();
		df.clear();
		bwDf.close();
		bwIdf.close();
		System.out.println("Dictionary Size: " + keySetCf.size());
		System.out.println("Dictionary Size: " + keySetDf.size());
	}

	public static void SortedFreqToString(
			HashMap<String, HashMap<Integer, LinkedList<String>>> freq,
			String path) throws IOException {
		HashMap<Integer, LinkedList<String>> dfSorted = freq.get("dfSorted");
		HashMap<Integer, LinkedList<String>> cfSorted = freq.get("cfSorted");
		Frequencies.getSort(dfSorted, path + "dfSorted");
		Frequencies.getSort(cfSorted, path + "cfSorted");
	}

	public static void getSort(HashMap<Integer, LinkedList<String>> map,
			String path) throws IOException {
		BufferedWriter bw = new BufferedWriter(new FileWriter(new File(path)));
		Vector<Integer> keys = new Vector<Integer>(map.keySet());
		Collections.sort(keys);
		for (int i = keys.size(); i > -1; i--) {
			Integer pos = keys.get(i);
			LinkedList<String> terms = map.get(pos);
			for (String t : terms) {
				bw.write(pos + " " + t);
			}
		}
		bw.close();
	}
}
